<!doctype html>



  


<html class="theme-next muse use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  
    
      
    

    
  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Microsoft YaHei:300,300italic,400,400italic,700,700italic|consolas:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Node.js," />








  <link rel="shortcut icon" type="image/x-icon" href="http://ofy7k3v4s.bkt.clouddn.com/mylogo.png?v=5.0.2" />






<meta name="description" content="fs &amp;#x6587;&amp;#x4EF6;&amp;#x64CD;&amp;#x4F5C;&amp;#x6A21;&amp;#x5757;
Node &amp;#x53EA;&amp;#x5728;&amp;#x6587;&amp;#x4EF6; IO &amp;#x64CD;&amp;#x4F5C;&amp;#x4E2D;&amp;#xFF0C;&amp;#x63D0;&amp;#x4F9B;&amp;#x4E86;&amp;#x540C;&amp;#x6B65;&amp;#x8C03;&amp;#x7528;&amp;#x548C;&amp;#x5F02;&amp;#x">
<meta property="og:type" content="article">
<meta property="og:title" content="Node-文件操作（上）-简单封装复制文件方法、process对象">
<meta property="og:url" content="https://hongqinma.github.io/2016/09/15/s002-node-文件操作（上）-简单封装复制文件方法、process对象/index.html">
<meta property="og:site_name" content="HongqinMa' Blog">
<meta property="og:description" content="fs &amp;#x6587;&amp;#x4EF6;&amp;#x64CD;&amp;#x4F5C;&amp;#x6A21;&amp;#x5757;
Node &amp;#x53EA;&amp;#x5728;&amp;#x6587;&amp;#x4EF6; IO &amp;#x64CD;&amp;#x4F5C;&amp;#x4E2D;&amp;#xFF0C;&amp;#x63D0;&amp;#x4F9B;&amp;#x4E86;&amp;#x540C;&amp;#x6B65;&amp;#x8C03;&amp;#x7528;&amp;#x548C;&amp;#x5F02;&amp;#x">
<meta property="og:updated_time" content="2016-11-30T11:40:55.423Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Node-文件操作（上）-简单封装复制文件方法、process对象">
<meta name="twitter:description" content="fs &amp;#x6587;&amp;#x4EF6;&amp;#x64CD;&amp;#x4F5C;&amp;#x6A21;&amp;#x5757;
Node &amp;#x53EA;&amp;#x5728;&amp;#x6587;&amp;#x4EF6; IO &amp;#x64CD;&amp;#x4F5C;&amp;#x4E2D;&amp;#xFF0C;&amp;#x63D0;&amp;#x4F9B;&amp;#x4E86;&amp;#x540C;&amp;#x6B65;&amp;#x8C03;&amp;#x7528;&amp;#x548C;&amp;#x5F02;&amp;#x">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '6347850919329138000',
      author: 'Hiraku'
    }
  };
</script>




  <link rel="canonical" href="https://hongqinma.github.io/2016/09/15/s002-node-文件操作（上）-简单封装复制文件方法、process对象/"/>


  <title> Node-文件操作（上）-简单封装复制文件方法、process对象 | HongqinMa' Blog </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="en">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?10010323";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta custom-logo">
  
    <div class="site-meta-headline">
      <a>
        <img class="custom-logo-image" src="http://ofy7k3v4s.bkt.clouddn.com/photo.jpg"
             alt="HongqinMa' Blog"/>
      </a>
    </div>
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">HongqinMa' Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">Spell at all costs， to rush you in the future。</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            About
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            Tags
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Node-文件操作（上）-简单封装复制文件方法、process对象
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">Posted on</span>
            <time itemprop="dateCreated" datetime="2016-09-15T19:40:41+08:00" content="2016-09-15">
              2016-09-15
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/Node-js/" itemprop="url" rel="index">
                    <span itemprop="name">Node.js</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2016/09/15/s002-node-文件操作（上）-简单封装复制文件方法、process对象/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/09/15/s002-node-文件操作（上）-简单封装复制文件方法、process对象/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          

          
              &nbsp; | &nbsp;
              <span class="page-pv"><i class="fa fa-file-o"></i>
              <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
              </span>
          

          
            <span id="busuanzi_container_page_pv">  <span id="busuanzi_value_page_pv"></span> Views</span>
          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>fs &#x6587;&#x4EF6;&#x64CD;&#x4F5C;&#x6A21;&#x5757;</p>
<p>Node &#x53EA;&#x5728;&#x6587;&#x4EF6; IO &#x64CD;&#x4F5C;&#x4E2D;&#xFF0C;&#x63D0;&#x4F9B;&#x4E86;&#x540C;&#x6B65;&#x8C03;&#x7528;&#x548C;&#x5F02;&#x6B65;&#x8C03;&#x7528;&#x4E24;&#x79CD;&#x5F62;&#x5F0F;&#xFF0C;fs &#x6A21;&#x5757;&#x5BF9;&#x6587;&#x4EF6;&#x7684;&#x51E0;&#x4E4E;&#x6240;&#x6709;&#x64CD;&#x4F5C;&#x90FD;&#x6709;&#x540C;&#x6B65;&#x548C;&#x5F02;&#x6B65;&#x4E24;&#x79CD;&#x5F62;&#x5F0F;&#xFF0C;&#x4E24;&#x8005;&#x53EF;&#x4EE5;&#x7ED3;&#x5408;&#x4F7F;&#x7528;&#xFF0C;&#x4F46;&#x662F;&#x63A8;&#x8350;&#x80FD;&#x4F7F;&#x7528;&#x5F02;&#x6B65;&#x8C03;&#x7528;&#x89E3;&#x51B3;&#x95EE;&#x9898;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5C11;&#x7528;&#x540C;&#x6B65;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF1A;<code>readFile()</code> &#x548C; <code>readFileSync()</code>&#x3002;</p>
<h2 id="&#x5F15;&#x5165;&#x5305;"><a href="#&#x5F15;&#x5165;&#x5305;" class="headerlink" title="&#x5F15;&#x5165;&#x5305;"></a>&#x5F15;&#x5165;&#x5305;</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&apos;path&apos;</span>);</div></pre></td></tr></table></figure>
<h2 id="&#x540C;&#x6B65;&#x64CD;&#x4F5C;"><a href="#&#x540C;&#x6B65;&#x64CD;&#x4F5C;" class="headerlink" title="&#x540C;&#x6B65;&#x64CD;&#x4F5C;"></a>&#x540C;&#x6B65;&#x64CD;&#x4F5C;</h2><ul>
<li>&#x540C;&#x6B65;&#x4EE3;&#x7801;&#x4F1A;&#x963B;&#x585E;&#x540E;&#x7EED;&#x4EE3;&#x7801;&#x6267;&#x884C;&#xFF0C;&#x6548;&#x7387;&#x4F4E;</li>
<li>&#x540C;&#x6B65; API &#x9700;&#x8981; try-catch &#x6355;&#x83B7;&#x5F02;&#x5E38;</li>
<li>&#x540C;&#x6B65; API &#x4F18;&#x70B9;&#x4EE3;&#x7801;&#x7B26;&#x5408;&#x601D;&#x7EF4;&#x903B;&#x8F91;&#xFF0C;</li>
<li>&#x6309;&#x987A;&#x5E8F;&#x6267;&#x884C;&#xFF0C;&#x7B80;&#x5355;&#x6267;&#x884C;</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="keyword">try</span> {</div><div class="line">  <span class="keyword">const</span> data = fs.readFileSync(<span class="string">&apos;./README.md&apos;</span>, <span class="string">&apos;utf8&apos;</span>);</div><div class="line">    <span class="built_in">console</span>.log(data);</div><div class="line">} <span class="keyword">catch</span>(e){</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&apos;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#x5931;&#x8D25;&apos;</span>);</div><div class="line">}</div></pre></td></tr></table></figure>
<p>&#x540C;&#x6B65;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#x7684;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="keyword">try</span> {</div><div class="line">  <span class="keyword">const</span> data1 = fs.readFileSync(<span class="string">&apos;./01.txt&apos;</span>, <span class="string">&apos;utf8&apos;</span>);</div><div class="line">  <span class="keyword">const</span> data2 = fs.readFileSync(<span class="string">&apos;./02.txt&apos;</span>, <span class="string">&apos;utf8&apos;</span>);</div><div class="line">  <span class="keyword">const</span> data2 = fs.readFileSync(<span class="string">&apos;./03.txt&apos;</span>, <span class="string">&apos;utf8&apos;</span>);</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&apos;&#x5DF2;&#x5B8C;&#x6210;&#x6240;&#x6709;&#x6570;&#x636E;&#x7684;&#x8BFB;&#x53D6;&apos;</span>);</div><div class="line">  <span class="built_in">console</span>.log(data1, data2, data3);</div><div class="line">} <span class="keyword">catch</span>(e){</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&apos;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#x5931;&#x8D25;&apos;</span>);</div><div class="line">}</div></pre></td></tr></table></figure>
<h2 id="&#x5F02;&#x6B65;&#x64CD;&#x4F5C;"><a href="#&#x5F02;&#x6B65;&#x64CD;&#x4F5C;" class="headerlink" title="&#x5F02;&#x6B65;&#x64CD;&#x4F5C;"></a>&#x5F02;&#x6B65;&#x64CD;&#x4F5C;</h2><p>&#x5F02;&#x6B65; API &#x5F80;&#x5F80;&#x4F34;&#x968F;&#x7740;&#x4E00;&#x4E2A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x7528;&#x6765;&#x63A5;&#x6536;&#xFF0C;&#x8FD4;&#x56DE;&#x503C;&#x6216;&#x5F02;&#x5E38;&#x5904;&#x7406;&#x3002;</p>
<p>&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x4E2D;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x4E00;&#x822C;&#x90FD;&#x662F;&#x4E00;&#x4E2A; err &#x5BF9;&#x8C61;&#xFF0C;&#x7528;&#x6765;&#x5224;&#x5B9A;&#x5F02;&#x6B65; API &#x662F;&#x5426;&#x53D1;&#x751F;&#x5F02;&#x5E38;&#x3002;</p>
<ul>
<li>&#x5F02;&#x6B65; API &#x5373;&#x4FBF;&#x6CA1;&#x6709;&#x6355;&#x83B7;&#x5F02;&#x5E38;&#x4E5F;&#x4E0D;&#x4F1A;&#x4E3B;&#x52A8;&#x629B;&#x51FA;&#x5F02;&#x5E38;</li>
<li>&#x5F02;&#x6B65; API&#xFF0C;&#x65E0;&#x6CD5;&#x901A;&#x8FC7; try-catch &#x6355;&#x83B7;&#x5F02;&#x5E38;</li>
</ul>
<p>&#x4E00;&#x822C;&#x6587;&#x4EF6;&#x64CD;&#x4F5C;&#x4E2D;&#xFF0C;&#x6240;&#x6709;&#x7684;&#x5F02;&#x6B65; API&#xFF0C;&#x90FD;&#x4F1A;&#x5728;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4E2D;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A; error &#x5BF9;&#x8C61;</p>
<ul>
<li>&#x5982;&#x679C;&#x64CD;&#x4F5C;&#x8FC7;&#x7A0B;&#x4E2D;&#x6709;&#x5F02;&#x5E38;&#xFF0C;&#x5219; error &#x662F;&#x4E00;&#x4E2A;&#x5F02;&#x5E38;&#x5BF9;&#x8C61;</li>
<li>&#x5982;&#x679C;&#x64CD;&#x4F5C;&#x6210;&#x529F;&#xFF0C;&#x6CA1;&#x6709;&#x95EE;&#x9898;&#xFF0C;&#x5219; error &#x662F;&#x4E00;&#x4E2A; null</li>
<li>&#x6240;&#x4EE5;&#xFF0C;&#x4E3A;&#x4E86;&#x5224;&#x5B9A;&#x662F;&#x5426;&#x6709;&#x5F02;&#x5E38;&#xFF0C;if (err) {// &#x5904;&#x7406;&#x5F02;&#x5E38;}</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line">fs.readFile(<span class="string">&quot;./data/01.txt&quot;</span>,<span class="string">&quot;utf8&quot;</span>, (err, data1) =&gt; {</div><div class="line">  <span class="keyword">if</span> (err) {</div><div class="line">    <span class="keyword">return</span> <span class="built_in">console</span>.log(<span class="string">&quot;&#x5931;&#x8D25;&#x4E86;&quot;</span>);</div><div class="line">  }</div><div class="line">  <span class="built_in">console</span>.log(data1);</div><div class="line">});</div></pre></td></tr></table></figure>
<p>&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x4E2D;&#xFF1A;</p>
<ul>
<li>&#x5F00;&#x53D1;&#x9636;&#x6BB5;&#xFF0C;&#x4F7F;&#x7528; throw err &#x7684;&#x5F62;&#x5F0F;&#x629B;&#x51FA;&#x5F02;&#x5E38;</li>
<li>&#x76EE;&#x7684;&#x662F;&#x4E3A;&#x4E86;&#x5FEB;&#x901F;&#x7684;&#x5B9A;&#x4F4D;&#x4EE3;&#x7801;&#x7684;&#x9519;&#x8BEF;</li>
<li>&#x5982;&#x679C;&#x662F;&#x7F51;&#x7AD9;&#x670D;&#x52A1;&#x5668;&#x4E2D;&#xFF0C;&#x8FD9;&#x4E2A;&#x5C31;&#x4E0D;&#x4F1A;&#x53BB; throw err&#xFF0C;</li>
<li>&#x4E00;&#x822C;&#x4F1A;&#x6709;&#x5F02;&#x5E38;&#x5904;&#x7406;&#x673A;&#x5236;</li>
<li>&#x4E00;&#x822C;&#x5728;&#x751F;&#x4EA7;&#x73AF;&#x5883;&#xFF0C;&#x4F1A;&#x5904;&#x7406;&#x5F02;&#x5E38;&#xFF0C;&#x4F8B;&#x5982;&#x8BB0;&#x5F55;&#x65E5;&#x5FD7;&#x65B9;&#x4FBF;&#x6392;&#x67E5;&#x9519;&#x8BEF;</li>
<li>throw err &#x4F1A;&#x76F4;&#x63A5;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#xFF0C;&#x9000;&#x51FA;&#x8FDB;&#x7A0B;</li>
</ul>
<h2 id="&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;"><a href="#&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;" class="headerlink" title="&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;"></a>&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="comment">// &#x5F00;&#x59CB;&#x505A;&#x67D0;&#x4E00;&#x4EF6;&#x4E8B;</span></div><div class="line"><span class="built_in">console</span>.log(<span class="string">&quot;&#x505A;&#x4E00;&#x4EF6;&#x4E8B;&quot;</span>);</div><div class="line"><span class="built_in">console</span>.log(<span class="string">&quot;&#x9700;&#x8981;&#x51C6;&#x5907;&#x4E00;&#x4E9B;&#x505A;&#x8FD9;&#x4EF6;&#x4E8B;&#x7684;&#x5FC5;&#x5907;&#x54C1;&quot;</span>);</div><div class="line"><span class="built_in">console</span>.log(<span class="string">&quot;&#x627E;&#x4EBA;&#x5E2E;&#x6211;&#x4E70;&#x5FC5;&#x5907;&#x54C1;\n\n&quot;</span>);</div><div class="line"><span class="comment">// &#x6240;&#x6709;&#x7684;&#x5F02;&#x6B65;&#x4EFB;&#x52A1;&#x90FD;&#x4F1A;&#x7B49;&#x5F85;&#x5176;&#x5B83;&#x7684;&#x540C;&#x6B65;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x7ED3;&#x675F;&#x4E4B;&#x540E;&#xFF0C;&#x624D;&#x4F1A;&#x5F00;&#x59CB;&#x6267;&#x884C;</span></div><div class="line">fs.readFile(<span class="string">&quot;./data/01.txt&quot;</span>, <span class="string">&quot;utf8&quot;</span>, (err, data1) =&gt; {</div><div class="line">  <span class="keyword">if</span> (err) {</div><div class="line">    <span class="keyword">throw</span> err;</div><div class="line">  }</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;11111111---&quot;</span>, data1);</div><div class="line">  fs.readFile(<span class="string">&quot;./data/02.txt&quot;</span>, <span class="string">&quot;utf8&quot;</span>, (err, data2) =&gt; {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">throw</span> err;</div><div class="line">    }</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;222222-----&quot;</span>, data2);</div><div class="line">    fs.readFile(<span class="string">&quot;./data/03.txt&quot;</span>, <span class="string">&quot;utf8&quot;</span>, (err, data3) =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">throw</span> err;</div><div class="line">      }</div><div class="line">      <span class="built_in">console</span>.log(<span class="string">&quot;3333----&quot;</span>, data3);</div><div class="line">      <span class="comment">// &#x5668;&#x6750;&#x5907;&#x9F50;&#xFF0C;&#x53EF;&#x4EE5;&#x505A;&#x4E8B;&#x4E86;</span></div><div class="line">      doSomething (data1, data2, data3);</div><div class="line">    });</div><div class="line">  });</div><div class="line">});</div><div class="line"></div><div class="line"><span class="built_in">console</span>.log(<span class="string">&quot;&#x5148;&#x5E72;&#x70B9;&#x522B;&#x7684;&#x6D3B;&quot;</span>);</div><div class="line"><span class="built_in">console</span>.log(<span class="string">&quot;&#x7136;&#x540E;&#x53BB;&#x7761;&#x4E00;&#x4F1A;&quot;</span>);</div><div class="line"><span class="built_in">console</span>.log(<span class="string">&quot;&#x6536;&#x62FE;&#x6536;&#x62FE;&#x51C6;&#x5907;&quot;</span>);</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">doSomething</span>(<span class="params">data1, data2, data3</span>) </span>{</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;ok, &#x5668;&#x6750;&#x5DF2;&#x7ECF;&#x51C6;&#x5907;&#x9F50;&#x5168;&#xFF0C;&#x53EF;&#x4EE5;&#x505A;&#x8FD9;&#x4EF6;&#x4E8B;&#x4E86;&quot;</span>);</div><div class="line">}</div></pre></td></tr></table></figure>
<h2 id="&#x5C01;&#x88C5;&#x51C6;&#x5907;&#x5668;&#x6750;&#x7684;&#x8FD9;&#x4E2A;&#x8FC7;&#x7A0B;&#xFF1B;"><a href="#&#x5C01;&#x88C5;&#x51C6;&#x5907;&#x5668;&#x6750;&#x7684;&#x8FD9;&#x4E2A;&#x8FC7;&#x7A0B;&#xFF1B;" class="headerlink" title="&#x5C01;&#x88C5;&#x51C6;&#x5907;&#x5668;&#x6750;&#x7684;&#x8FD9;&#x4E2A;&#x8FC7;&#x7A0B;&#xFF1B;"></a>&#x5C01;&#x88C5;&#x51C6;&#x5907;&#x5668;&#x6750;&#x7684;&#x8FD9;&#x4E2A;&#x8FC7;&#x7A0B;&#xFF1B;</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="comment">// &#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x7684;&#x5C01;&#x88C5;</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">ready</span>(<span class="params">doSomething</span>)</span>{</div><div class="line">  fs.readFile(<span class="string">&quot;./data/01.txt&quot;</span>, <span class="string">&quot;utf8&quot;</span>, (err, data1) =&gt; {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">throw</span> err;</div><div class="line">    }</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;11111111---&quot;</span>, data1);</div><div class="line">    fs.readFile(<span class="string">&quot;./data/02.txt&quot;</span>, <span class="string">&quot;utf8&quot;</span>, (err, data2) =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">throw</span> err;</div><div class="line">      }</div><div class="line">      <span class="built_in">console</span>.log(<span class="string">&quot;222222-----&quot;</span>, data2);</div><div class="line">      fs.readFile(<span class="string">&quot;./data/03.txt&quot;</span>, <span class="string">&quot;utf8&quot;</span>, (err, data3) =&gt; {</div><div class="line">        <span class="keyword">if</span> (err) {</div><div class="line">          <span class="keyword">throw</span> err;</div><div class="line">        }</div><div class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;3333----&quot;</span>, data3);</div><div class="line">        <span class="comment">// &#x5668;&#x6750;&#x5907;&#x9F50;&#xFF0C;&#x53EF;&#x4EE5;&#x505A;&#x4E8B;&#x4E86;</span></div><div class="line">        doSomething (data1, data2, data3);</div><div class="line">      });</div><div class="line">    });</div><div class="line">  });</div><div class="line">}</div><div class="line"></div><div class="line">ready(<span class="function">(<span class="params">data1, data2, data3</span>) =&gt;</span> {</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;ok, &#x5668;&#x6750;&#x5DF2;&#x7ECF;&#x51C6;&#x5907;&#x9F50;&#x5168;&#xFF0C;&#x53EF;&#x4EE5;&#x505A;&#x8FD9;&#x4EF6;&#x4E8B;&#x4E86;&quot;</span>);</div><div class="line">});</div></pre></td></tr></table></figure>
<h2 id="&#x4FEE;&#x6539;&#x5C01;&#x88C5;&#x4E2D;&#x7684;&#x5F02;&#x5E38;&#x5904;&#x7406;&#x65B9;&#x5F0F;"><a href="#&#x4FEE;&#x6539;&#x5C01;&#x88C5;&#x4E2D;&#x7684;&#x5F02;&#x5E38;&#x5904;&#x7406;&#x65B9;&#x5F0F;" class="headerlink" title="&#x4FEE;&#x6539;&#x5C01;&#x88C5;&#x4E2D;&#x7684;&#x5F02;&#x5E38;&#x5904;&#x7406;&#x65B9;&#x5F0F;"></a>&#x4FEE;&#x6539;&#x5C01;&#x88C5;&#x4E2D;&#x7684;&#x5F02;&#x5E38;&#x5904;&#x7406;&#x65B9;&#x5F0F;</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="comment">// &#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x7684;&#x5C01;&#x88C5;</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">ready</span>(<span class="params">doSomething</span>)</span>{</div><div class="line">  fs.readFile(<span class="string">&quot;./data/01.txt&quot;</span>, <span class="string">&quot;utf8&quot;</span>, (err, data1) =&gt; {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">return</span> doSomething(err);</div><div class="line">    }</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;11111111---&quot;</span>, data1);</div><div class="line">    fs.readFile(<span class="string">&quot;./data/02.txt&quot;</span>, <span class="string">&quot;utf8&quot;</span>, (err, data2) =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">return</span> doSomething(err);</div><div class="line">      }</div><div class="line">      <span class="built_in">console</span>.log(<span class="string">&quot;222222-----&quot;</span>, data2);</div><div class="line">      fs.readFile(<span class="string">&quot;./data/03.txt&quot;</span>, <span class="string">&quot;utf8&quot;</span>, (err, data3) =&gt; {</div><div class="line">        <span class="keyword">if</span> (err) {</div><div class="line">          <span class="keyword">return</span> doSomething(err);</div><div class="line">        }</div><div class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;3333----&quot;</span>, data3);</div><div class="line">        <span class="comment">// &#x4E0D;&#x53D1;&#x751F;&#x9519;&#x8BEF;&#x65F6;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x4E00;&#x4E2A; null</span></div><div class="line">        doSomething (<span class="literal">null</span>, data1, data2, data3);</div><div class="line">      });</div><div class="line">    });</div><div class="line">  });</div><div class="line">}</div><div class="line"><span class="comment">// &#x5C01;&#x88C5;&#x8FC7;&#x540E;&#x5904;&#x7406;&#x9519;&#x8BEF;&#x7684;&#x8FC7;&#x7A0B;</span></div><div class="line">ready(<span class="function">(<span class="params">err, data1, data2, data3</span>) =&gt;</span> {</div><div class="line">  <span class="keyword">if</span> (err) {</div><div class="line">    <span class="keyword">throw</span> err;</div><div class="line">  }</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;ok, &#x5668;&#x6750;&#x5DF2;&#x7ECF;&#x51C6;&#x5907;&#x9F50;&#x5168;&#xFF0C;&#x53EF;&#x4EE5;&#x505A;&#x8FD9;&#x4EF6;&#x4E8B;&#x4E86;&quot;</span>);</div><div class="line">});</div></pre></td></tr></table></figure>
<h2 id="&#x5C01;&#x88C5;&#x591A;&#x4E2A;&#x5F02;&#x6B65;&#x5E76;&#x884C;&#x4EFB;&#x52A1;"><a href="#&#x5C01;&#x88C5;&#x591A;&#x4E2A;&#x5F02;&#x6B65;&#x5E76;&#x884C;&#x4EFB;&#x52A1;" class="headerlink" title="&#x5C01;&#x88C5;&#x591A;&#x4E2A;&#x5F02;&#x6B65;&#x5E76;&#x884C;&#x4EFB;&#x52A1;"></a>&#x5C01;&#x88C5;&#x591A;&#x4E2A;&#x5F02;&#x6B65;&#x5E76;&#x884C;&#x4EFB;&#x52A1;</h2><p>&#x627E;&#x591A;&#x4E2A;&#x4EBA;&#x533A;&#x505A;&#x51C6;&#x5907;&#x5DE5;&#x4F5C;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="comment">// &#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x7684;&#x5C01;&#x88C5;</span></div><div class="line"><span class="keyword">let</span> count = <span class="number">0</span>;</div><div class="line"><span class="comment">// &#x7B2C;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;</span></div><div class="line">fs.readFile(<span class="string">&quot;./data/01.txt&quot;</span>, <span class="string">&quot;utf8&quot;</span>, (err, data1) =&gt; {</div><div class="line">  <span class="keyword">if</span> (err) {</div><div class="line">    <span class="keyword">throw</span> err;</div><div class="line">  }</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;11111111---&quot;</span>, data1);</div><div class="line">  count++;</div><div class="line">  <span class="keyword">if</span> (count === <span class="number">3</span>){</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;ok, &#x5668;&#x6750;&#x5DF2;&#x7ECF;&#x51C6;&#x5907;&#x9F50;&#x5168;&#xFF0C;&#x53EF;&#x4EE5;&#x505A;&#x8FD9;&#x4EF6;&#x4E8B;&#x4E86;&quot;</span>);</div><div class="line">  }</div><div class="line">});</div><div class="line"><span class="comment">// &#x7B2C;&#x4E8C;&#x4E2A;&#x4EFB;&#x52A1;</span></div><div class="line">fs.readFile(<span class="string">&quot;./data/02.txt&quot;</span>, <span class="string">&quot;utf8&quot;</span>, (err, data2) =&gt; {</div><div class="line">  <span class="keyword">if</span> (err) {</div><div class="line">    <span class="keyword">throw</span> err;</div><div class="line">  }</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;222222-----&quot;</span>, data2);</div><div class="line">  count++;</div><div class="line">  <span class="keyword">if</span> (count === <span class="number">3</span>){</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;ok, &#x5668;&#x6750;&#x5DF2;&#x7ECF;&#x51C6;&#x5907;&#x9F50;&#x5168;&#xFF0C;&#x53EF;&#x4EE5;&#x505A;&#x8FD9;&#x4EF6;&#x4E8B;&#x4E86;&quot;</span>);</div><div class="line">  }</div><div class="line">});</div><div class="line"><span class="comment">// &#x7B2C;&#x4E09;&#x4E2A;&#x4EFB;&#x52A1;</span></div><div class="line">fs.readFile(<span class="string">&quot;./data/03.txt&quot;</span>, <span class="string">&quot;utf8&quot;</span>, (err, data3) =&gt; {</div><div class="line">  <span class="keyword">if</span> (err) {</div><div class="line">    <span class="keyword">throw</span> err;</div><div class="line">  }</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;3333----&quot;</span>, data3);</div><div class="line">  count++;</div><div class="line">  <span class="keyword">if</span> (count === <span class="number">3</span>){</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;ok, &#x5668;&#x6750;&#x5DF2;&#x7ECF;&#x51C6;&#x5907;&#x9F50;&#x5168;&#xFF0C;&#x53EF;&#x4EE5;&#x505A;&#x8FD9;&#x4EF6;&#x4E8B;&#x4E86;&quot;</span>);</div><div class="line">  }</div><div class="line">});</div></pre></td></tr></table></figure>
<p>&#x5C06;&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x7EE7;&#x7EED;&#x5C01;&#x88C5;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="comment">// &#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x7684;&#x5C01;&#x88C5;</span></div><div class="line"><span class="keyword">let</span> count = <span class="number">0</span>;</div><div class="line"><span class="comment">// &#x7B2C;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;</span></div><div class="line">fs.readFile(<span class="string">&quot;./data/01.txt&quot;</span>, <span class="string">&quot;utf8&quot;</span>, (err, data1) =&gt; {</div><div class="line">  <span class="keyword">if</span> (err) {</div><div class="line">    <span class="keyword">throw</span> err;</div><div class="line">  }</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;11111111---&quot;</span>, data1);</div><div class="line">  doSomething();</div><div class="line">});</div><div class="line"><span class="comment">// &#x7B2C;&#x4E8C;&#x4E2A;&#x4EFB;&#x52A1;</span></div><div class="line">fs.readFile(<span class="string">&quot;./data/02.txt&quot;</span>, <span class="string">&quot;utf8&quot;</span>, (err, data2) =&gt; {</div><div class="line">  <span class="keyword">if</span> (err) {</div><div class="line">    <span class="keyword">throw</span> err;</div><div class="line">  }</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;222222-----&quot;</span>, data2);</div><div class="line">  doSomething();</div><div class="line">});</div><div class="line"><span class="comment">// &#x7B2C;&#x4E09;&#x4E2A;&#x4EFB;&#x52A1;</span></div><div class="line">fs.readFile(<span class="string">&quot;./data/03.txt&quot;</span>, <span class="string">&quot;utf8&quot;</span>, (err, data3) =&gt; {</div><div class="line">  <span class="keyword">if</span> (err) {</div><div class="line">    <span class="keyword">throw</span> err;</div><div class="line">  }</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;3333----&quot;</span>, data3);</div><div class="line">  doSomething();</div><div class="line">});</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">doSomething</span>(<span class="params"></span>)</span>{</div><div class="line">  count++;</div><div class="line">  <span class="keyword">if</span> (count === <span class="number">3</span>){</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;ok, &#x5668;&#x6750;&#x5DF2;&#x7ECF;&#x51C6;&#x5907;&#x9F50;&#x5168;&#xFF0C;&#x53EF;&#x4EE5;&#x505A;&#x8FD9;&#x4EF6;&#x4E8B;&#x4E86;&quot;</span>);</div><div class="line">  }</div><div class="line">}</div></pre></td></tr></table></figure>
<p>&#x4EE5;&#x4E0A;&#x5C01;&#x88C5;&#x7684;&#x95EE;&#x9898;&#xFF0C;ready &#x7684;&#x8FC7;&#x7A0B;&#x53EF;&#x80FD;&#x5931;&#x8D25;&#xFF0C;&#x5E94;&#x8BE5;&#x544A;&#x8BC9;&#x505A;&#x4E8B;&#x7684;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x4E3B;&#x52A8;&#x629B;&#x51FA;&#x6765;&#xFF1B;&#x600E;&#x4E48;&#x544A;&#x8BC9;&#x5462;&#xFF1F;&#x4F20;&#x7ED9;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x4E8E;&#x662F;&#x5C01;&#x88C5;&#x6210;&#x4E86;&#x4E0B;&#x9762;&#x4EE3;&#x7801;:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&apos;path&apos;</span>);</div><div class="line"></div><div class="line">ready([<span class="string">&apos;./data/01.txt&apos;</span>, <span class="string">&apos;./data/02.txt&apos;</span>, <span class="string">&apos;./data/03.txt&apos;</span>], (err, obj) =&gt; {</div><div class="line">  <span class="comment">// obj { data1:&apos;&apos;, data2: &apos;&apos;, data3: &apos;&apos; }</span></div><div class="line">  <span class="keyword">if</span> (err) {</div><div class="line">    <span class="keyword">return</span> <span class="built_in">console</span>.log(<span class="string">&apos;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x5931;&#x8D25;&#x4E86;&apos;</span>);</div><div class="line">  }</div><div class="line">  <span class="built_in">console</span>.log(obj);</div><div class="line">})</div><div class="line"></div><div class="line"><span class="comment">// &#x5C01;&#x88C5;&#x591A;&#x4E2A;&#x5E76;&#x884C;&#x7684;&#x5F02;&#x6B65;API</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">ready</span>(<span class="params">filePaths, callback</span>) </span>{</div><div class="line">  <span class="keyword">const</span> obj = {};</div><div class="line">  <span class="keyword">const</span> length = filePaths.length;</div><div class="line">  <span class="keyword">let</span> count = <span class="number">0</span>;</div><div class="line">  <span class="comment">// &#x5F53;&#x5FAA;&#x73AF;&#x6267;&#x884C;&#x7ED3;&#x675F;&#xFF0C;&#x5C31;&#x8BF4;&#x660E;&#x5206;&#x914D;&#x4E86;&#x4E09;&#x4E2A;&#x5F02;&#x6B65;&#x4EFB;&#x52A1;</span></div><div class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> filePath <span class="keyword">of</span> filePaths) {</div><div class="line">    fs.readFile(filePath, <span class="string">&apos;utf8&apos;</span>, (err, data) =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">return</span> callback(err);</div><div class="line">      }</div><div class="line">      obj[path.parse(filePath).name] = data;</div><div class="line">      count++;</div><div class="line">      <span class="keyword">if</span> (count === length) {</div><div class="line">        callback(<span class="literal">null</span>, obj);</div><div class="line">      }</div><div class="line">    })</div><div class="line">  }</div><div class="line">}</div></pre></td></tr></table></figure>
<h2 id="&#x6B4C;&#x8BCD;&#x5728;&#x63A7;&#x5236;&#x53F0;&#x4E00;&#x884C;&#x4E00;&#x884C;&#x6253;&#x5370;-lrc"><a href="#&#x6B4C;&#x8BCD;&#x5728;&#x63A7;&#x5236;&#x53F0;&#x4E00;&#x884C;&#x4E00;&#x884C;&#x6253;&#x5370;-lrc" class="headerlink" title="&#x6B4C;&#x8BCD;&#x5728;&#x63A7;&#x5236;&#x53F0;&#x4E00;&#x884C;&#x4E00;&#x884C;&#x6253;&#x5370; lrc"></a>&#x6B4C;&#x8BCD;&#x5728;&#x63A7;&#x5236;&#x53F0;&#x4E00;&#x884C;&#x4E00;&#x884C;&#x6253;&#x5370; lrc</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="keyword">const</span> iconv = <span class="built_in">require</span>(<span class="string">&apos;iconv-lite&apos;</span>);</div><div class="line">fs.readFile(<span class="string">&apos;./data/168305.lrc&apos;</span>, (err, data) =&gt; {</div><div class="line">  <span class="keyword">if</span> (err) {</div><div class="line">    <span class="keyword">throw</span> err;</div><div class="line">  }</div><div class="line">  <span class="keyword">const</span> lrcStr = iconv.decode(data, <span class="string">&apos;gbk&apos;</span>);</div><div class="line">  <span class="keyword">const</span> lines = lrcStr.split(<span class="string">&apos;\r\n&apos;</span>);</div><div class="line">  <span class="keyword">const</span> regex = <span class="regexp">/^\[(\d{2})\:(\d{2})\.(\d{2})\]\s(.+)$/</span>;</div><div class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> line <span class="keyword">of</span> lines){</div><div class="line">    <span class="keyword">const</span> matches = regex.exec(line);</div><div class="line">    <span class="keyword">if</span> (matches) {</div><div class="line">      <span class="keyword">const</span> m = <span class="built_in">parseInt</span>(matches[<span class="number">1</span>]);</div><div class="line">      <span class="keyword">const</span> s = <span class="built_in">parseInt</span>(matches[<span class="number">2</span>]);</div><div class="line">      <span class="keyword">const</span> ms = <span class="built_in">parseInt</span>(matches[<span class="number">3</span>]);</div><div class="line">      <span class="keyword">const</span> content = matches[<span class="number">4</span>];</div><div class="line">      <span class="keyword">const</span> time = m * <span class="number">60</span> * <span class="number">1000</span> + s * <span class="number">1000</span> + ms;</div><div class="line">      setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</div><div class="line">        <span class="built_in">console</span>.log(content);</div><div class="line">      }, time);</div><div class="line">    }</div><div class="line">  }</div><div class="line">});</div></pre></td></tr></table></figure>
<ul>
<li>&#x540C;&#x6B65;&#x8C03;&#x7528;&#x7ACB;&#x5373;&#x6267;&#x884C;&#xFF0C;&#x4F1A;&#x963B;&#x585E;&#x540E;&#x7EED;&#x4EE3;&#x7801;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#xFF0C;&#x5982;&#x679C;&#x60F3;&#x8981;&#x6355;&#x83B7;&#x5F02;&#x5E38;&#x9700;&#x8981;&#x4F7F;&#x7528; <code>try-catch</code></li>
<li>&#x5F02;&#x6B65;&#x8C03;&#x7528;&#x4E0D;&#x4F1A;&#x963B;&#x585E;&#x540E;&#x7EED;&#x4EE3;&#x7801;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#xFF0C;&#x9700;&#x8981;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x989D;&#x5916;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x901A;&#x5E38;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x4F5C;&#x4E3A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;</li>
<li>&#x5F02;&#x6B65;&#x8C03;&#x7528;&#x901A;&#x8FC7;&#x5224;&#x65AD;&#x7B2C;&#x4E00;&#x4E2A;err&#x5BF9;&#x8C61;&#x6765;&#x5904;&#x7406;&#x5F02;&#x5E38;</li>
<li>&#x5F02;&#x6B65;&#x8C03;&#x7528;&#x7ED3;&#x679C;&#x5F80;&#x5F80;&#x901A;&#x8FC7;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x6765;&#x8FDB;&#x884C;&#x83B7;&#x53D6;</li>
</ul>
<h2 id="&#x590D;&#x5236;&#x6587;&#x4EF6;"><a href="#&#x590D;&#x5236;&#x6587;&#x4EF6;" class="headerlink" title="&#x590D;&#x5236;&#x6587;&#x4EF6;"></a>&#x590D;&#x5236;&#x6587;&#x4EF6;</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line">copy (<span class="string">&apos;./node.md&apos;</span>,<span class="string">&apos;./a.md&apos;</span>, err =&gt; {</div><div class="line">  <span class="keyword">if</span> (err) {</div><div class="line">    <span class="keyword">throw</span> err;</div><div class="line">  } </div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&apos;copy success&apos;</span>);</div><div class="line">});</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">copy</span>(<span class="params">src, dest, callback</span>) </span>{</div><div class="line">  fs.readFile(src, (err, data) =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">return</span> callback(err);</div><div class="line">      }</div><div class="line">      fs.writeFile(dest, data, err =&gt; {</div><div class="line">        <span class="keyword">if</span> (err){</div><div class="line">          <span class="keyword">return</span> callback(err);</div><div class="line">        }</div><div class="line">        callback(<span class="literal">null</span>);</div><div class="line">      });</div><div class="line">  });</div><div class="line">}</div></pre></td></tr></table></figure>
<h2 id="process"><a href="#process" class="headerlink" title="process"></a>process</h2><p>process.argv &#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x901A;&#x8FC7;&#x6267;&#x884C;&#x811A;&#x672C;&#x7684;&#x65F6;&#x5019;&#x4F20;&#x9012;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x9ED8;&#x8BA4;&#x7ED3;&#x679C;&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;</p>
<ul>
<li>&#x6570;&#x7EC4;&#x4E2D;&#x7B2C; 0 &#x9879;&#x5C31;&#x662F; node &#x7684;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;</li>
<li>&#x6570;&#x7EC4;&#x4E2D;&#x7B2C; 1 &#x9879;&#x5C31;&#x662F; &#x6267;&#x884C;&#x7684;&#x5F53;&#x524D;&#x811A;&#x672C;&#x6587;&#x4EF6;&#x7684;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;</li>
<li>&#x6570;&#x7EC4;&#x4E2D;&#x4ECE;&#x7B2C; 2 &#x9879;&#x5F00;&#x59CB;&#xFF0C;&#x5C31;&#x662F;&#x7528;&#x6237;&#x901A;&#x8FC7;&#x6267;&#x884C;&#x547D;&#x4EE4;&#x4F20;&#x9012;&#x8FDB;&#x6765;&#x7684;&#x53C2;&#x6570;&#x9009;&#x9879;&#xFF0C;&#x4EE5;&#x7A7A;&#x683C;&#x5212;&#x5206;</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> args = process.argv.slice(<span class="number">2</span>);</div></pre></td></tr></table></figure>
<p>&#x5C01;&#x88C5;&#x590D;&#x5236;&#x6587;&#x4EF6;&#x51FD;&#x6570;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="keyword">const</span> args = process.argv.slice(<span class="number">2</span>);</div><div class="line">copy (args[<span class="number">0</span>], args[<span class="number">1</span>], err =&gt; {</div><div class="line">  <span class="keyword">if</span> (err) {</div><div class="line">    <span class="keyword">throw</span> err;</div><div class="line">  }</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&apos;copy success!&apos;</span>);</div><div class="line">});</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">copy</span>(<span class="params">src, des, callback</span>) </span>{</div><div class="line">  fs.readFile(src, (err, data) =&gt; {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">return</span> callback(err);</div><div class="line">    }</div><div class="line">    fs.writeFile(des, data, err =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">return</span> callback(err);</div><div class="line">      }</div><div class="line">      callback(<span class="literal">null</span>);</div><div class="line">    });</div><div class="line">  });</div><div class="line">}</div></pre></td></tr></table></figure>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>感谢您的支持！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>打 赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="http://ofy7k3v4s.bkt.clouddn.com/wcpayCode.jpg" alt="Hongqin Ma WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="http://ofy7k3v4s.bkt.clouddn.com/apayCode.jpg" alt="Hongqin Ma Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Node-js/" rel="tag">#Node.js</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/09/13/s001-node-node常用API/" rel="next" title="Node常用API">
                <i class="fa fa-chevron-left"></i> Node常用API
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/09/19/s003-node-promise-对象/" rel="prev" title="Node-promise 对象">
                Node-promise 对象 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <div class="ds-share flat" data-thread-key="2016/09/15/s002-node-文件操作（上）-简单封装复制文件方法、process对象/"
     data-title="Node-文件操作（上）-简单封装复制文件方法、process对象"
     data-content=""
     data-url="https://hongqinma.github.io/2016/09/15/s002-node-文件操作（上）-简单封装复制文件方法、process对象/">
  <div class="ds-share-inline">
    <ul  class="ds-share-icons-16">

      <li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
      <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
      <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
      <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
      <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>

    </ul>
    <div class="ds-share-icons-more">
    </div>
  </div>
</div>
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/09/15/s002-node-文件操作（上）-简单封装复制文件方法、process对象/"
           data-title="Node-文件操作（上）-简单封装复制文件方法、process对象" data-url="https://hongqinma.github.io/2016/09/15/s002-node-文件操作（上）-简单封装复制文件方法、process对象/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Overview
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/photo.jpg"
               alt="Hongqin Ma" />
          <p class="site-author-name" itemprop="name">Hongqin Ma</p>
          <p class="site-description motion-element" itemprop="description">Spell at all costs， to rush you in the future. </p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">68</span>
              <span class="site-state-item-name">posts</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">16</span>
                <span class="site-state-item-name">categories</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">57</span>
                <span class="site-state-item-name">tags</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/HongqinMa" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/HongqinMa" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.facebook.com/profile.php?id=100006581058735" target="_blank" title="Facebook">
                  
                    <i class="fa fa-fw fa-facebook"></i>
                  
                  Facebook
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.zhihu.com/people/HirakuMa" target="_blank" title="知乎">
                  
                    <i class="fa fa-fw fa-share-alt"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/rubylouvre/" title="司徒正美" target="_blank">司徒正美</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/sanshi/" title="三生石上" target="_blank">三生石上</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/TomXu/" title="汤姆大叔" target="_blank">汤姆大叔</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://gold.xitu.io/" title="掘金" target="_blank">掘金</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://segmentfault.com/" title="segmentfault" target="_blank">segmentfault</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.jianshu.com/users/1cbb94e5ed4d/latest_articles" title="简书" target="_blank">简书</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://home.cnblogs.com/" title="博客园" target="_blank">博客园</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://www.awesomes.cn/" title="Awesomes" target="_blank">Awesomes</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#引入包"><span class="nav-number">1.</span> <span class="nav-text">引入包</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#同步操作"><span class="nav-number">2.</span> <span class="nav-text">同步操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#异步操作"><span class="nav-number">3.</span> <span class="nav-text">异步操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#一个例子"><span class="nav-number">4.</span> <span class="nav-text">一个例子</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#封装准备器材的这个过程；"><span class="nav-number">5.</span> <span class="nav-text">封装准备器材的这个过程；</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#修改封装中的异常处理方式"><span class="nav-number">6.</span> <span class="nav-text">修改封装中的异常处理方式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#封装多个异步并行任务"><span class="nav-number">7.</span> <span class="nav-text">封装多个异步并行任务</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#歌词在控制台一行一行打印-lrc"><span class="nav-number">8.</span> <span class="nav-text">歌词在控制台一行一行打印 lrc</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#复制文件"><span class="nav-number">9.</span> <span class="nav-text">复制文件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#process"><span class="nav-number">10.</span> <span class="nav-text">process</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Hongqin Ma</span>
</div>

<div class="powered-by">
  Powered by <a class="theme-link" href="https://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span id="busuanzi_container_site_pv">
    &nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;Page Views: <span id="busuanzi_value_site_pv"></span> 
</span>

        

<div class="busuanzi-count">

  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv"><i class="fa fa-user"></i><span class="busuanzi-value" id="busuanzi_value_site_uv"></span></span>
  

  
    <span class="site-pv"><i class="fa fa-eye"></i><span class="busuanzi-value" id="busuanzi_value_site_pv"></span></span>
  
  
</div>



        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"Hiraku"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  

  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  
  <!-- custom analytics part create by xiamo -->
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
<script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
<script>
function showTime(Counter) {
  var query = new AV.Query(Counter);
  $(".leancloud_visitors").each(function() {
    var url = $(this).attr("id").trim();
    query.equalTo("url", url);
    query.find({
      success: function(results) {
        if (results.length == 0) {
          var content = '0 ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
          return;
        }
        for (var i = 0; i < results.length; i++) {
          var object = results[i];
          var content = object.get('time') + ' ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
        }
      },
      error: function(object, error) {
        console.log("Error: " + error.code + " " + error.message);
      }
    });

  });
}

function addCount(Counter) {
  var Counter = AV.Object.extend("Counter");
  url = $(".leancloud_visitors").attr('id').trim();
  title = $(".leancloud_visitors").attr('data-flag-title').trim();
  var query = new AV.Query(Counter);
  query.equalTo("url", url);
  query.find({
    success: function(results) {
      if (results.length > 0) {
        var counter = results[0];
        counter.fetchWhenSave(true);
        counter.increment("time");
        counter.save(null, {
          success: function(counter) {
            var content =  counter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(counter, error) {
            console.log('Failed to save Visitor num, with error message: ' + error.message);
          }
        });
      } else {
        var newcounter = new Counter();
        newcounter.set("title", title);
        newcounter.set("url", url);
        newcounter.set("time", 1);
        newcounter.save(null, {
          success: function(newcounter) {
              console.log("newcounter.get('time')="+newcounter.get('time'));
            var content = newcounter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(newcounter, error) {
            console.log('Failed to create');
          }
        });
      }
    },
    error: function(error) {
      console.log('Error:' + error.code + " " + error.message);
    }
  });
}
$(function() {
  var Counter = AV.Object.extend("Counter");
  if ($('.leancloud_visitors').length == 1) {
    addCount(Counter);
  } else if ($('.post-title-link').length > 1) {
    showTime(Counter);
  }
}); 
</script>

  
</body>
</html>
